/**

 @Name：layuiAdmin 用户登入和注册等
 @Author：贤心
 @Site：http://www.layui.com/admin/
 @License: LPPL
    
 */
 
layui.define('form', function(exports){
  var $ = layui.$
  ,layer = layui.layer
  ,laytpl = layui.laytpl
  ,setter = layui.setter
  ,view = layui.view
  ,admin = layui.admin
  ,form = layui.form
  ,router = layui.router()
  ,search = router.search;

  var $body = $('body');
  
  //图形验证码


  //更换图形验证码
  $body.on('click', '#LAY-user-get-vercode', function(){
    var othis = $(this);
    var key = new Date().getTime();
    this.src = setter.api_captcha + '?verifykey=' + key;
    $("#LAY-user-login-verkey").val(key);
  });

  //提交
  form.on('submit(LAY-user-login-submit)', function(obj){
    //请求登入接口
    admin.req({
      url: setter.api_login
      ,data: obj.field
      ,type: 'post'
      ,done: function(res){
        //请求成功后，写入 access_token
        layui.data(setter.tableName, {
          key: setter.request.tokenName
          ,value: res.data.access_token
        });
        //登入成功的提示与跳转
        layer.msg('登入成功', {
          offset: '15px'
          ,icon: 1
          ,time: 1000
        }, function(){
          location.hash = search.redirect ? decodeURIComponent(search.redirect) : '/';
        });
      }
    });
  });
  //对外暴露的接口
  exports('login', {});
});